<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;
use app\api\common\Program;
use think\Config;
use think\Db;
use think\Validate;

use app\common\model\Typeone;
use app\common\model\Typetwo;
use app\common\model\Goods;
use app\common\model\Collect;
use app\common\model\Zhanting as ZhantingModel;

/**
 * 展厅接口
 */
class Zhanting extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取展厅列表
     */
    public function getZhanTingList()
    {
        $keyword = $this->request->param('keyword/s', '');
        if ($keyword) {
            $where = [
                'name' => ['like', "%$keyword%"],
                'status' => '1'
            ];
        } else {
            $where = [
                'status' => '1'
            ];
        }
        $data = ZhantingModel::where($where)
            ->field("id,name,tel,province_city,urban_city,district_city,address,wechat_image,businesshours")
            ->select();
        foreach ($data as $key => $value) {
            $data[$key]['province_city_text'] = Db::name('area')->where('id', $value['province_city'])->value('name');
            $data[$key]['urban_city_text'] = Db::name('area')->where('id', $value['urban_city'])->value('name');
            $data[$key]['district_city_text'] = Db::name('area')->where('id', $value['district_city'])->value('name');
            $data[$key]['allAddress_text'] = $value['province_city_text'] . $value['urban_city_text'] . $value['district_city_text'] . $value['address'];
        }
        $this->success('请求成功', $data);
    }
    /**
     * 获取展厅详情
     */
    public function getZhanTingDetail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('缺少展厅id');
        }
        $data = ZhantingModel::get($id);
        if (!$data) {
            $this->error('展厅不存在');
        }
        $data = $data->toArray();
        unset($data['status']);
        unset($data['createtime']);
        unset($data['updatetime']);
        unset($data['weigh']);
        unset($data['status_text']);
        $data['province_city_text'] = Db::name('area')->where('id', $data['province_city'])->value('name');
        $data['urban_city_text'] = Db::name('area')->where('id', $data['urban_city'])->value('name');
        $data['district_city_text'] = Db::name('area')->where('id', $data['district_city'])->value('name');
        $data['allAddress_text'] = $data['province_city_text'] . $data['urban_city_text'] . $data['district_city_text'] . $data['address'];
        $data['images'] = explode(',', $data['images']);
        foreach ($data['images'] as $key => $value) {
            $data['images'][$key] = cdnurl($value, true);
        }
        $data['content'] = getHandleRichtext($data['content']);
        $this->success('请求成功', $data);
    }
    /**
     * 搜索关联
     */
    public function getSearchKey()
    {
        $keyword = $this->request->param('keyword/s', '');
        if (!$keyword) {
            $this->error('缺少搜索关键字');
        }
        $where = [
            'name' => ['like', "%$keyword%"],
            'status' => '1'
        ];
        $data = ZhantingModel::where($where)
            ->column('name');
        $this->success('请求成功', $data);
    }
}
